﻿using System;
using UnityEngine;
namespace Assets.Common.Math
{
    /// <summary>
    /// 双计数类型, 一般用于当前 / 上限
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// 
    [Serializable]
    public class Fraction<T> where T : struct
    {
        [SerializeField]
        private T _member; // 分子
        [SerializeField]
        private T _denominator; // 分母

        public Fraction(T member, T denominator)
        {
            _member = member;
            _denominator = denominator;
        }
        /// <summary>
        /// 分子
        /// </summary>
        public T Member { get => _member; set => _member = value; }
        /// <summary>
        /// 分母
        /// </summary>
        public T Denominator { get => _denominator; set => _denominator = value; }

        public override string ToString()
        {
            return $"{_member}/{_denominator}";
        }
    }
    public static class FractionEx
    {
        /// <summary>
        /// 进度计算,a/b,b=0时定义为不需要执行直接完成进度
        /// </summary>
        /// <param name="fraction"></param>
        /// <returns></returns>
        public static float Progress(this Fraction<float> fraction)
        {
            if (fraction.Denominator == 0) return 1;
            return fraction.Member / fraction.Denominator;
        }
        public static float Progress(this Fraction<int> fraction)
        {
            if (fraction.Denominator == 0) return 1;
            return (float)fraction.Member / fraction.Denominator;
        }
        /// <summary>
        /// 简化为真分式,长用于循环显示的进度
        /// </summary>
        /// <param name="fraction"></param>
        /// <returns></returns>
        public static float NormalizeProgress(this Fraction<float> fraction)
        {
            if (fraction.Denominator == 0) return 1;
            return (fraction.Member % fraction.Denominator) / fraction.Denominator;
        }
        public static float NormalizeProgress(this Fraction<int> fraction)
        {
            if (fraction.Denominator == 0) return 1;
            return (float)(fraction.Member % fraction.Denominator) / fraction.Denominator;
        }
        /// <summary>
        /// 对分数进行减一操作,常用于溢出进位
        /// </summary>
        /// <param name="fraction"></param>
        /// <returns></returns>
        public static void ProgressMinusOne(this Fraction<float> fraction)
        {
            fraction.Member -= fraction.Denominator;
        }
        public static void ProgressMinusOne(this Fraction<int> fraction)
        {
            fraction.Member -= fraction.Denominator;
        }
        /// <summary>
        /// 分母减分子,常用于当前进度的倒计时值
        /// </summary>
        /// <param name="fraction"></param>
        /// <returns></returns>
        public static float ProgressCountDown(this Fraction<float> fraction)
        {
            return fraction.Denominator - fraction.Member;
        }
        public static float ProgressCountDown(this Fraction<int> fraction)
        {
            return fraction.Denominator - fraction.Member;
        }
        public static void Reset(this Fraction<float> fraction)
        {
            fraction.Member = 0f;
        }
        public static void Reset(this Fraction<int> fraction)
        {
            fraction.Member = 0;
        }
    }


}